/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.annotations.Columns;

/**
 *
 * @author unnati
 */
@Table(name = "answers")
@Entity
public class Answer  extends AbstractEntity
{
    @GeneratedValue
    
    @Column(name = "id")
    @Id
    private Long id;
    
    @Column(name = "sid")
    private Long sid;

    @Column(name = "qid")
    private Long qid;
    
    @Column(name = "tid")
    private Long tid;
    
    @Column(name = "sanswer")
    private String sanswer;
    
    @Transient
    public Question question;

    public Question getQuestion() {
        return question;
    }

    public void setQuestion( ) {
        this.question = (Question)new Question().getById(qid);
    }
    
    

    public Long getSid() {
        return sid;
    }

    public void setSid(Long sid) {
        this.sid = sid;
    }

    public Long getQid() {
        return qid;
    }

    public void setQid(Long qid) {
        this.qid = qid;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public String getSanswer() {
        return sanswer;
    }

    public void setSanswer(String sanswer) {
        this.sanswer = sanswer;
    }
    
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public static List<Answer> GetAllAnswerOfDivision(Long testid,Long divid)
    {
        List<Answer> list_answer=null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Answer where tid="+testid+" and sid in(select id from Student where divid="+divid+")");
            list_answer=q.list();
            
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        
        return list_answer;
    }
    public  static  List<Answer> GetAllAnswerOfStudent(Long testid,Long stuid)
    {
        List<Answer> li_ans=null;
        
        try
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Answer where tid="+testid+" and sid="+stuid+"");
            li_ans=q.list();
            
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        
        return li_ans;
    }
    public  static  List<Long> GetNullAns(Long testid,Long stuid)
    {
        List<Long> l=null;
        try {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Answer where tid="+testid+" and sid="+stuid+" and sanswer=null");
           l=q.list();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return l;
    }
}
